package com.base.myproject.client.busi.print;

import com.base.myproject.client.system.BaseResource;
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AbstractImagePrototype;

/*
 * 这个类主要用来打印报表
 * @param jspUrl 根据gwt客户端传过来的jspUrl寻找hr_Jsp下对应的jsp文件生成模板，因为
 * 要根据传过来的jspUrl建立对应的jsp文件。
 * frame.el().setElementAttribute("src","hr_Jsp/rpt" + jspUrl+ ".jsp?data=" + data);
 * @param data gwt客户端传过来的查询条件
 */
public class ReportPrintDialog implements ClickHandler,Listener<BaseEvent>{

	public Dialog d = new Dialog();
	com.extjs.gxt.ui.client.widget.FramePanel frame = com.extjs.gxt.ui.client.widget.FramePanel.pop();
	boolean autoprint = false;
	boolean autocancel = false;
	int autoprintmsecond = 7;
	Html message = new Html("");
	private String jspUrl;
	private String data;
	
	public ReportPrintDialog(){
		init();
	}
	
	public void init(){
		initWidget();
	}
	
	public void initWidget(){
		frame.setId("__printingFrameX");
		frame.setBorders(false);
		
		d.setHeading("打印");
		d.setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.print_16()));
		d.setLayout(new RowLayout(Orientation.HORIZONTAL));  
		d.add(frame);
		d.setSize(1034, 520);

		d.setButtons(Dialog.YESNO);  
		d.getButtonById(Dialog.YES).setText("打印");
		d.getButtonById(Dialog.NO).setText("取消");
		d.getButtonById(Dialog.YES).setIcon(AbstractImagePrototype.create(BaseResource.INSTANCE.print_16()));
		
		d.getButtonById(Dialog.YES).addListener(Events.Select, this);
		d.getButtonById(Dialog.NO).addListener(Events.Select, this);
		if(autoprint)
			d.getButtonBar().add(message);
		
		d.addDomHandler(new ClickHandler(){
			@Override
			public void onClick(ClickEvent event) {
				autocancel=true;
			}} , ClickEvent.getType());
	}
	public com.extjs.gxt.ui.client.widget.FramePanel getFramePanel(){
		com.extjs.gxt.ui.client.widget.FramePanel frame=this.frame;
		return frame;
	}
	
	public void show(){
		d.show();
		d.setFocusWidget(d.getButtonById(Dialog.YES));
		frame.el().setSize(1020, 450, true);
		load();
		if(autoprint){
		 Timer t = new Timer() {
			 int i = 0;
		      public void run() {
		    	  i++;
		    	  if(i >= autoprintmsecond){
		    		  cancel(); 
		    		  print();
		    		  message.setHtml("<a style=\"font-size: 14pt;\"><font color=\"#ff0000\">打印中</font> </a>");
		    	  }
		    	  if(autocancel){
		    		  cancel(); 
		    		  message.setHtml("");			    		
		    	  }
		    	  else{
		    		  message.setHtml("<a style=\"font-size: 14pt;\"><font color=\"#ff0000\">在"+(autoprintmsecond-i)+"秒后自动打印</font> </a>");
		    	  }
		      }
		    };
		    t.scheduleRepeating(1000);
		}
	}
	
	@Override
	public void onClick(ClickEvent event) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void handleEvent(BaseEvent be) {
		// TODO Auto-generated method stub
		if(be.getSource().equals(d.getButtonById(Dialog.NO))){
			d.hide();
		}
		else if(be.getSource().equals(d.getButtonById(Dialog.YES))){
			print();
		}
	}
	
	public void print(){
		
		 if (GXT.isIE) {
			 printFrame_IE();
		 }
		 else{
			 printFrame();
		 }
	}

	//非ie下的打印
    public static native void printFrame() /*-{
		var iframe = $doc.frames ? $doc.frames['__printingFrameX'] : $doc.getElementById('__printingFrameX');
	    var ifWin = iframe.contentWindow || iframe;
	    ifWin.print();
	}-*/;


    //ie下的frame打印
    public static native void printFrame_IE()/*-{
    	$wnd.frames['__printingFrameX'].focus();
    	$wnd.frames['__printingFrameX'].print();
	}-*/;

	public void load(){
		frame.el().setElementAttribute("src","hr_Jsp/rpt" + jspUrl+ ".jsp?data=" + data);
		System.out.println("*******************" + "hr_Jsp/rpt" + jspUrl+ ".jsp?data=" + data);
    }
    
	public String getJspUrl() {
		return jspUrl;
	}

	public void setJspUrl(String jspUrl) {
		this.jspUrl = jspUrl;
	}

	public String getData() {
		return data;
	}

	public void setData(String data) {
		this.data = data;
	}
	
}
